﻿using System.Linq;

namespace Pap.Web.Modules.Reports.Views
{
    using System;

    using Coolite.Ext.Web;
    using Pap.Web.Modules.Reports.ViewModel;
    using Pap.Web.Utils;

    /// <summary>
    /// 
    /// </summary>
    public partial class MonthlyResponsibleReport : BasePage<MonthlyResponsibleReportViewModel>
    {
        #region [ Events ]

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.GenerateReport += Master_GenerateReport;
            if (!IsPostBack && !Ext.IsAjaxRequest)
            {
                FillddlCommittee();
                FillddlUsers();
            }
        }

        protected void Master_GenerateReport(object sender, EventArgs e)
        {
            Search();
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the ddlCommittee control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ddlCommittee_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillddlUsers();
        }

        #endregion

        #region [ Methods ]

        /// <summary>
        /// Fillddls the committee.
        /// </summary>
        private void FillddlCommittee()
        {
            ddlCommittee.Items.Clear();
            strCommittee.DataSource = ViewModel.GetAllCommittees();
            strCommittee.DataBind();
        }

        /// <summary>
        /// Fillddls the users.
        /// </summary>
        private void FillddlUsers()
        {
            ddlUsers.Items.Clear();

            if (ddlCommittee.SelectedItem != null)
            {
                strUsers.DataSource = ViewModel.GetAllResponsibleUsers(ddlCommittee.SelectedItem.Value.ToGuid());
                strUsers.DataBind();
            }
        }

        /// <summary>
        /// Searches this instance.
        /// </summary>
        private void Search()
        {
            strReport.DataSource = this.ViewModel.GetMonthlyResponisbleReport(ddlCommittee.SelectedItem.Value.ToGuid(),
                                                                             ddlUsers.SelectedItem.Value.ToGuid(),
                                                                             dtFromDate.SelectedDate,
                                                                             dtToDate.SelectedDate);
            strReport.DataBind();

            //rpvMonthlyResponisbleReport.Visible = true;
            //CommitteeID = new Guid(ddlCommittee.SelectedItem.Value);

            //if (CommitteeID != Guid.Empty)
            //    UserID = new Guid(ddlUsers.SelectedItem.Value);

            //FromDate = dtFromDate.SelectedDate;
            //ToDate = dtToDate.SelectedDate;

            //ReportDataSource datasource = new ReportDataSource("dsMonthlyResponsibleReports",
            //       ViewModel.GetMonthlyResponisbleReport(UserID, CommitteeID, FromDate, ToDate).Tables[0]);

            //rpvMonthlyResponisbleReport.LocalReport.DataSources.Clear();
            //rpvMonthlyResponisbleReport.LocalReport.DataSources.Add(datasource);
            //rpvMonthlyResponisbleReport.ZoomMode = ZoomMode.PageWidth;
            //rpvMonthlyResponisbleReport.ShowZoomControl = true;
            //rpvMonthlyResponisbleReport.Attributes["DIR"] = "LTR";
            //rpvMonthlyResponisbleReport.LocalReport.Refresh();
        }

        #endregion
    }
}